package com.atguigu.guli.service.base.handler;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Date 2021/5/18 14:33
 * @Description TODO
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
//提供异常处理器
    //1、最大的异常处理
@ExceptionHandler(value = Exception.class)
    public R exception(Exception e){
    System.out.println("ExceptionUtils = " + ExceptionUtils.getStackTrace(e));
    return R.error();
}

    //2、具体异常的处理
@ExceptionHandler(value = BadSqlGrammarException.class)
    public R error(Exception e){
    System.out.println("ExceptionUtils.getStackTrace(e) = " + ExceptionUtils.getStackTrace(e));
    return R.setResultCodeEnum(ResultCodeEnum.BAD_SQL_GRAMMAR);
}


}

